import java.util.Stack;

public class Solution1081 {
    public String smallestSubsequence(String s) {
        Stack<Character> stack=new Stack<>();
        int[] last=new int[26];
        int[] visit=new int[26];
        for (int i=0;i<s.length();i++){
            last[s.charAt(i)-'a']=i;
        }
        for (int i=0;i<s.length();i++){
            if (visit[s.charAt(i)-'a']==1){
                continue;
            }
            while (!stack.isEmpty() && stack.peek()>=s.charAt(i) && last[stack.peek()-'a']>=i){
                visit[stack.pop()-'a']=0;
            }
            stack.push(s.charAt(i));
            visit[s.charAt(i)-'a']=1;
        }
        StringBuilder stringBuilder=new StringBuilder();
        for (Character character : stack) {
            stringBuilder.append(character);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution1081().smallestSubsequence("bcabc"));
    }
}
